/**
 * @(#)SetSubscribeController.java, 2010-5-30. 
 * 
 * Copyright 2010 Youdao, Inc. All rights reserved.
 * YOUDAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.rsstuan.web.controller;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.rsstuan.storage.impl.JDOTuanGouItemStorage;

/**
 *
 * @author guosq
 *
 */
public class SetScoreController  extends SpringController {
    
    private static final Logger LOG = Logger.getLogger(SetScoreController.class.getName());
    private JDOTuanGouItemStorage itemStorage = new JDOTuanGouItemStorage();
    
    public ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        
            String idStr = request.getParameter("id");
            String scoreStr = request.getParameter("score");
            if (!StringUtils.isBlank(idStr) && !StringUtils.isBlank(scoreStr)) {
                try {
                    Long id = Long.parseLong(idStr);
                    Double score = Double.parseDouble(scoreStr);
                    
                    
                    double avgScore = itemStorage.updateScore(id, score);
                    int round = (int) (avgScore * 2);
                    avgScore = (double)round / 2;
                   renderText(response, String.valueOf(avgScore));
                   response.setStatus(200);
                } catch (Exception e) {
                    LOG.log(Level.WARNING, "fail to updateScore", e);
                }
            }
            return null;
    }
    
}
